package com.lechi.web.controller;

import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.service.IIotAlertRecordService;
import com.lechi.web.service.IIotDeviceCmdService;
import com.lechi.web.service.IIotDeviceSensorRelService;
import com.lechi.web.service.IIotTriggerRulesService;
import com.lechi.web.system.domain.IotDeviceSensorRelBo;
import com.lechi.web.system.domain.IotTriggerRules;
import com.lechi.web.vo.IotDeviceSensorRelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 设备关联观测要素关系Controller
 *
 * @author yangguang
 */
@RestController
@RequestMapping("/iot/device/sensor")
@RequiredArgsConstructor
public class IotDeviceSensorRelController extends BaseController {

    private final IIotDeviceSensorRelService iotDeviceSensorRelService;

    private final IIotDeviceCmdService iotDeviceCmdService;

    private final IIotTriggerRulesService iIotTriggerRulesService;

    private final IIotAlertRecordService iotAlertRecordService;

    /**
     * 查询设备关联观测要素关系列表
     *
     * @param iotDeviceSensorRelV the iot device sensor rel v
     * @return the table data info
     */
    @GetMapping("/list")
    public TableDataInfo list(IotDeviceSensorRelVo iotDeviceSensorRelV) {
        startPage();
        return alarmlist(iotDeviceSensorRelV);
    }

    /**
     * 告警规则页面，查询设备关联观测要素列表
     *
     * @param iotDeviceSensorRelV the iot device sensor rel v
     * @return the table data info
     */
    @GetMapping("/alarmlist")
    //    @PreAuthorize("@ss.hasPermi('iot:device:query')")
    public TableDataInfo alarmlist(IotDeviceSensorRelVo iotDeviceSensorRelV) {
        startPage();
        IotDeviceSensorRelBo iotDeviceGroupRel = new IotDeviceSensorRelBo();
        iotDeviceGroupRel.setDeviceId(iotDeviceSensorRelV.getDeviceId());
        List<IotDeviceSensorRelBo> retList = iotDeviceSensorRelService.selectIotDeviceSensorRelListByDeviceId(iotDeviceGroupRel);
        return getDataTable(retList);
    }

    /**
     * 删除设备关联观测要素关系
     *
     * @param sensorAliasIds the sensor alias ids
     * @return the ajax result
     */
    @DeleteMapping("/{sensorAliasIds}")
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public AjaxResult remove(@PathVariable Long[] sensorAliasIds) {
        Arrays.asList(sensorAliasIds).forEach(sensorAliasId -> {
            iotDeviceCmdService.deleteIotDeviceCmdBySensorAliasId(sensorAliasId);
            iIotTriggerRulesService.deleteIotTriggerRulesBySensorAliasId(sensorAliasId);
            iotAlertRecordService.deleteIotAlertRecordBySensorAliasId(sensorAliasId);
            IotTriggerRules iotTriggerRules = new IotTriggerRules();
            iotTriggerRules.setSensorAliasId(sensorAliasId);
            iIotTriggerRulesService.cleanCacheAlarmRule(iotTriggerRules);

        });
        iotDeviceSensorRelService.deleteIotDeviceSensorRelByIds(sensorAliasIds);
        return AjaxResult.success();
    }
}
